package com.atguigu.msm.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msm.service.MsmService;
import com.atguigu.msm.utils.RandomUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class MsmServiceImpl implements MsmService {
    private static final String accessKeyId = "LTAI4G8WCvacCJG2PrnJFxsA";
    private static final String accessKeySecret = "2nLv08CpPnaaTvAu5LjSXfYeZTMH2y";
    //    签名
    private static final String SignName = "启明星";

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String sendMessage(String phoneNumber) {


        ValueOperations<String, String> value = redisTemplate.opsForValue();
//        如果存在 5分钟之内不发送 直接返回
        String s = value.get(phoneNumber);
        if (!StringUtils.isEmpty(s)) {
            return s;
        }
//        获取随机验证码
        String random = RandomUtil.getFourBitRandom();
        //获取指定位数验证码  可替代自定义类 该工具来源自common-lang3
//        String randomNumeric = RandomStringUtils.randomNumeric(4);

        value.set(phoneNumber, random, 5, TimeUnit.MINUTES);
//        存进map集合 进行json转换
        HashMap<String, String> map = Maps.newHashMap();
        map.put("code", random);
//        发送短信
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("TemplateCode", "SMS_195862415");
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        //验证码信息 一定要使用JSON格式
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));
        map.clear();
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return random;
    }
}
